﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DAL;

namespace TTCN_QLDiemC3.MyUserControl.UCHienThi
{
    public partial class UCSuaHS : UserControl
    {
        HocSinhBUO hsBUO = new HocSinhBUO();
        int id;
        public string id_click;
        public UCSuaHS(String id_hs)
        {
            InitializeComponent();
            cblop.Items.Clear();
            cbgioitinh.Items.Clear();
            List<string> list = hsBUO.getclassname();
            foreach (string lop in list) cblop.Items.Add(lop);

            cbgioitinh.Items.Add("Nam");
            cbgioitinh.Items.Add("Nữ");

            tbl_HocSinh tbl_hs = hsBUO.getstudentbyidhs(id_hs);
            tbmahs.Text = tbl_hs.id_HocSinh;
            tbhoten.Text = tbl_hs.HoTenHS;

            cbgioitinh.Text = tbl_hs.GioiTinh;
            tbdiachi.Text = tbl_hs.DiaChi;
            tbdienthoai.Text = tbl_hs.SDT;
            dateTimePicker1.Text = tbl_hs.NgaySinh;

            tbl_Lop tbl_lop = hsBUO.getstudentbyidlop(tbl_hs.id_Lop);
            cblop.Text = tbl_lop.TenLop;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = hsBUO.showallstudent();
        }

        private void btLamlai_Click(object sender, EventArgs e)
        {
            tbhoten.Text = null;
            tbdiachi.Text = null;
            tbdienthoai.Text = null;
            //tbgioitinh.Text = null;
            //cblop.Items.Clear();
            dateTimePicker1.Text = null;

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (tbhoten.Text == "" || tbdiachi.Text == "" || tbdienthoai.Text == "")
            {
                MessageBox.Show("Cần nhập đầy đủ thông tin !");
            }
            else
            {
                tbl_HocSinh obj_hs = new tbl_HocSinh();
                obj_hs.HoTenHS = tbhoten.Text;
                obj_hs.DiaChi = tbdiachi.Text;
                obj_hs.GioiTinh = cbgioitinh.Text;
                obj_hs.SDT = tbdienthoai.Text;
                obj_hs.NgaySinh = dateTimePicker1.Text;
                String id_hocsinh = tbmahs.Text;

                String classname = cblop.Text;
                tbl_Lop obj_lop = hsBUO.getclassbyclassname(classname);

                obj_hs.id_Lop = obj_lop.id_Lop;
                //tbl_Lop obj_lop = new tbl_Lop();
                //obj_lop.TenLop = tblop.Text;
                //String id_Lop = hsBUO.getstudentbyidhs(id_hocsinh).id_Lop;
                if (hsBUO.UpdateStudent(id_hocsinh, obj_hs))
                {
                    MessageBox.Show("Edit is OK");
                    dataGridView1.AutoGenerateColumns = false;
                    dataGridView1.DataSource = hsBUO.showallstudent();
                }
                else
                {
                    MessageBox.Show("Edit is not OK");
                    dataGridView1.AutoGenerateColumns = false;
                    dataGridView1.DataSource = hsBUO.showallstudent();
                }
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                id = e.RowIndex;
                id_click = dataGridView1.Rows[id].Cells[0].Value.ToString();


                tbl_HocSinh tbl_hs = hsBUO.getstudentbyidhs(id_click);
                tbmahs.Text = tbl_hs.id_HocSinh;
                tbhoten.Text = tbl_hs.HoTenHS;
                cbgioitinh.Text = tbl_hs.GioiTinh;
                tbdiachi.Text = tbl_hs.DiaChi;
                tbdienthoai.Text = tbl_hs.SDT;
                dateTimePicker1.Text = tbl_hs.NgaySinh;
                tbl_Lop tbl_lop = hsBUO.getstudentbyidlop(tbl_hs.id_Lop);
                cblop.Text = tbl_lop.TenLop;
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = hsBUO.showallstudent();
            }
            catch (System.Exception ex)
            {

            }

        }

        private void danhSáchHọcSinhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCDanhSachHS lisths = new UCDanhSachHS();
            Form fm = this.ParentForm;
            ControlAccess.ShowConTrol(fm, lisths, "grbHienThiChucNang");
        }

        private void thêmHọcSinhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCThemHocSinh addStudent = new UCThemHocSinh();
            Form fm = this.ParentForm;
            ControlAccess.ShowConTrol(fm, addStudent, "grbHienThiChucNang");

        }

        private void xóaHọcSinhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn có chắc chắn muốn xóa", "Xóa dữ liệu học sinh", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                if (hsBUO.delstudent(id_click))
                {
                    MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Xóa thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                dataGridView1.DataSource = hsBUO.showallstudent();
            }
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {

                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);

                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // hiển thị ContextMenuStrip tại vị trí X,Y 
                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));
                        // lấy vị trí hàng và giá trị của mã học sinh tại hàng đó
                        id = dataGridView1.HitTest(e.X, e.Y).RowIndex;
                        id_click = dataGridView1.Rows[id].Cells[0].Value.ToString();

                    }
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }
    }
}
